package com.lzp.badminton.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzp.badminton.config.ResultMessage;
import com.lzp.badminton.model.Activityjoin;
import com.lzp.badminton.service.IActivityjoinService;
import com.lzp.badminton.vo.ActivityClubJoin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2022-02-11
 */
@RestController
@RequestMapping("/activityjoin")
public class ActivityjoinController {
    @Autowired
    private ResultMessage resultMessage;
    @Autowired
    private IActivityjoinService iActivityjoinService;

    @GetMapping(value = "/getActivityJoin")
    @ResponseBody
    public ResultMessage getActivityJoin(@RequestParam("activityId") Integer activityId) {
        List<Activityjoin> activityjoinList = iActivityjoinService.getActivityJoin(activityId);

        if (activityjoinList == null) {
            resultMessage.success("200", "当前活动的报名人数为0", null);
            return resultMessage;
        } else {
            resultMessage.success("200", "获取报名数据成功", activityjoinList);
            return resultMessage;
        }
    }

    @GetMapping(value = "/getJoinNum")
    @ResponseBody
    public ResultMessage getJoinNum(@RequestParam("activityId") Integer activityId) {
        int result = iActivityjoinService.getJoinNum(activityId);

        Map joinNum = new HashMap();
        joinNum.put("joinNum", result);

        resultMessage.success("200", "查询活动报名人数成功", joinNum);
        return resultMessage;
    }

    @GetMapping(value = "/getJoinActivityUser")
    @ResponseBody
    public ResultMessage getJoinActivityUser(@RequestParam("activityId") Integer activityId, @RequestParam("userId") String userId) {
        Activityjoin aj = iActivityjoinService.getJoinActivityUser(userId, activityId);

        if (aj != null) {
            resultMessage.success("200", "该用户已参加了该活动", aj);
        } else {
            resultMessage.success("404", "该用户没有参加这个活动", null);
        }

        return resultMessage;
    }

    @GetMapping(value = "/cancelUserJoin")
    @ResponseBody
    public ResultMessage cancelUserJoin(@RequestParam("activityId") Integer activityId, @RequestParam("userId") String userId) {
        int result = iActivityjoinService.cancelUserJoin(userId, activityId);

        if (result == 0) {
            resultMessage.fail("404", "取消报名失败");
        } else {
            resultMessage.success("200", "取消报名成功");
        }

        return resultMessage;
    }

    @GetMapping(value = "/userJoinActivity")
    @ResponseBody
    public ResultMessage userJoinActivity(@RequestParam("activityId") Integer activityId, @RequestParam("userId") String userId, @RequestParam("joinNum") int joinNum) {
        int result = iActivityjoinService.userJoinActivity(userId, activityId, joinNum);

        if (result == 0) {
            resultMessage.fail("404", "报名失败");
        } else if (result == -1){
            resultMessage.fail("404", "报名失败, 人数大于限制");
        } else if (result == 1) {
            resultMessage.success("200", "报名成功");
        }

        return resultMessage;
    }
}
